package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Api(tags = "通用接口")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {

        log.info("文件上传:{}", file.getOriginalFilename());

        //将文件上传到阿里云OSS上存储，得到URL地址
        String url = null;
        try {
            url = aliOssUtil.upload(file);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("文件上传失败");
        }
        return Result.success(url);


    }
}
